/**
 *
 */
package com.newer.controller.sys.permission;

import com.newer.biz.sys.auth.ShiroBiz;
import com.newer.biz.sys.menu.MenuInfoBiz;
import com.newer.biz.sys.permission.PermissionBiz;
import com.newer.commons.result.BizResult;
import com.newer.dao.bean.sys.Permission;
import com.newer.dao.commons.dataobject.MenuPermissionDO;
import com.newer.utils.ShiroAuthUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 权限信息相关Controller
 *
 * @author xuntj
 * @create 2018-05-21 18:45
 **/
@Controller
@RequestMapping("sys/permission")
public class PermissionController {

    /**
     * 权限信息相关业务操作类
     */
    @Autowired
    PermissionBiz permissionBiz;

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/list.htm")
    public String role() {
        return "sys/permission/permission_list";
    }

    /**
     * 权限信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/doList.do")
    @ResponseBody
    public String roleList(Permission permission) {
        BizResult<Permission> bizResult = permissionBiz.queryPermissionInfoByPage(permission);
        return JSONObject.fromObject(bizResult).toString();
    }


    /**
     * 添加权限信息页面渲染
     *
     * @return
     */
    @RequestMapping("/operate.htm")
    public String operate(String id, ModelMap map) {
        // 查询权限信息
        if (StringUtils.isNotBlank(id)) {
            BizResult<Permission> result = permissionBiz.queryPermissionById(id);
            if (result.isSuccess()) {
                map.put("permission", result.getResult());
            }
        }
        return "sys/permission/permission_operate";
    }

    /**
     * 权限信息操作
     *
     * @return
     */
    @RequestMapping("/doOperate.do")
    @ResponseBody
    public String doOperate(Permission permission) {
        BizResult<?> bizResult = permissionBiz.operatePermissionInfo(permission);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 权限信息校验
     *
     * @return
     */
    @RequestMapping("/doCheck.do")
    @ResponseBody
    public String doCheck(String permissionCode) {
        BizResult<?> bizResult = permissionBiz.checkPermissionCodeExits(permissionCode);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 权限信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/doAll.do")
    @ResponseBody
    public String doAll(MenuPermissionDO permission) {
        BizResult<Permission> bizResult = permissionBiz.queryMenuDefaultPermissionInfo(permission);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 权限信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/validStatus.do")
    @ResponseBody
    public String validStatus(String id, Integer status) {
        BizResult<?> bizResult = permissionBiz.validStatus(id, status);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 权限信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/doRoleMenuPermissions.do")
    @ResponseBody
    public String doMenuPermissions(String roleId, Integer permissionStatus, @RequestParam("menuId[]") List<String> menuIds) {
        BizResult<Permission> bizResult = permissionBiz.queryRoleMenuPermissionsInfo(roleId, permissionStatus, menuIds);
        return JSONObject.fromObject(bizResult).toString();
    }
}
